﻿using AutoMapper;
using B.T.BasicData.Domain.RBAC;
using B.T.BasicData.ErrorCode;
using B.T.BasicData.Infraeturcture;
using B.T.BasicData.Write_Api.DTO;
using B.T.BasicData.Write_Api.RBAC.IServices;

namespace B.T.BasicData.Write_Api.RBAC.Services
{
    public class PermissionsServices : IPermissionsServices
    {
        private readonly ILogger<PermissionsServices> logger;
        private readonly IMapper mapper;
        private readonly IBaseRepository<Permissions> permissions;

        public PermissionsServices(ILogger<PermissionsServices> logger,IMapper mapper,IBaseRepository<Permissions> permissions)
        {
            this.logger = logger;
            this.mapper = mapper;
            this.permissions = permissions;
        }

        /// <summary>
        /// 权限添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult> AddPermissions(AddPermissionsDto dto)
        {
            try
            {
                logger.LogInformation("权限信息添加成功");
                var res=new ApiResult();

                var model = mapper.Map<Permissions>(dto);

                var info=await permissions.AddAnyx(model);

                if (info > 0)
                {
                    res.success("权限信息添加成功");
                    return res;
                }

                return res;

            }
            catch (Exception ex)
            {

                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// 权限删除
        /// </summary>
        /// <returns></returns>
        public async Task<ApiResult> DelePermissions(DelePermissionsdto dto)
        {
            try
            {
                logger.LogInformation("权限信息删除操作");
                var res = new ApiResult();

                var info = await permissions.GetValue(x => x.PermissionsId.Equals(dto.PermissionsId));

                if (info != null)
                {
                    info.IsDeleted = true;
                    var model = await permissions.Update(info);

                    if (model > 0)
                    {
                        res.success("删除成功");
                        return res;
                    }
                }
                return res;
            }
            catch (Exception ex)
            {

                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }


        /// <summary>
        /// 查找权限所有名称
        /// </summary>
        /// <returns></returns>
        public async Task<ApiResult<List<SearchPermissionsDto>>> SearchPermissionsinfo()
        {
            try
            {
                logger.LogInformation("查找权限所有名称操作");
                var res = new ApiResult<List<SearchPermissionsDto>>();

                var Permissionsse = permissions.Getlist().Where(x => x.IsDeleted.Equals(false)).Select(x=>new SearchPermissionsDto
                {
                    PermissionsId = x.PermissionsId,
                    PermissionName = x.PermissionName,

                }).ToList();

                res.success(Permissionsse);
                res.msg = "查找权限所有名称成功";
                return res;
            }
            catch (Exception ex)
            {

                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// 权限显示
        /// </summary>
        /// <returns></returns>
        public async Task<ApiResult<List<Permissions>>> SreachPermissions(string ? PermissionName)
        {
            try
            {

                var res=new ApiResult<List<Permissions>>();

                var info = permissions.Getlist().Where(x => x.IsDeleted.Equals(false));

                if (!string.IsNullOrEmpty(PermissionName))
                {
                    info = info.Where(x => x.PermissionName.Contains(PermissionName));
                }


                res.success(info.ToList());
                res.msg = "查找权限信息成功";
                return res;


            }
            catch (Exception ex)
            {

                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// 修改权限信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ApiResult> UpdatePermissions(UpdatePermissionsDto dto)
        {
           var res=new ApiResult();

            var mode = mapper.Map<Permissions>(dto);

            var info = await permissions.Update(mode);

            if (info > 0)
            {
                res.success("权限修改成功");
                return res;
            }

            return res;
        }
    }
}
